//
//  SystemSetViewController.m
//  MIDI
//
//  Created by E.M.Game on 15/9/15.
//  Copyright (c) 2015年 QXSX. All rights reserved.
//

#import "SystemSetViewController.h"
#import "AppConfig.h"
#import "SystemCommonCell.h"
#import "SystemBigCell.h"
#import "MessageNotifyViewController.h"
#import "UIViewController+Create.h"
#import "RecvAddressViewController.h"
#import "UIViewController+NavItem.h"
#import "UIViewController+Create.h"
#import "SIAlertView.h"
#import "UserDataSingleton.h"
#import "ContactMIDIController.h"

@interface SystemSetViewController ()<UITableViewDataSource , UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (weak, nonatomic) IBOutlet UIView *menuView;
@property (weak, nonatomic) IBOutlet UIView *menuItemView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *menuBottomConstraint;
@property (assign , nonatomic) BOOL isAnimed; //是否正在播放动画
@property (assign , nonatomic) BOOL isOpenMenu;//是否已经打开菜单
@end

@implementation SystemSetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.myTableView.scrollEnabled = NO;
    self.menuBottomConstraint.constant = -101;
    self.menuView.hidden = YES;
    self.isAnimed = NO;
    self.isOpenMenu = NO;
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapBg:)];
    [self.menuView addGestureRecognizer:tapGestureRecognizer];
}

-(void)viewWillAppear:(BOOL)animated
{
    [self configNavigationItem];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

-(void) configNavigationItem
{
    [self.navigationController.navigationBar setBarTintColor:MIDIColorOfNavigationBar];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.navigationItem.title = @"系统设置";
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor, [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0],UITextAttributeTextShadowColor,[UIFont fontWithName:@"Hiragino Sans GB" size:18.0],UITextAttributeFont,nil]];
    
    [self configNavigationBackAction:^{
        
        [self.navigationController popViewControllerAnimated:YES];
    }];
}

-(void)pressLeft:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark tableView datasource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 0)
    {
        return 72;
    }
    else if(indexPath.row == 1 || indexPath.row == 3)
    {
        return 51;
    }
    else if(indexPath.row == 2)
    {
        return 72;
    }
    return 60;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 0 || indexPath.row == 2)
    {
        SystemBigCell *cell = [SystemBigCell loadFromNib];
        
        if(indexPath.row == 0)
        {
            [cell.label setText:@"收货地址"];
            
            cell.viewHeightConstaint.constant = 19.0;
            
        }else
        {
            [cell.label setText:@"联系我们"];
        }
//        cell.button.tag = indexPath.row;
//        [cell.button addTarget:self  action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    }
    else
    {
        SystemCommonCell * cell = [SystemCommonCell loadFromNib];
        if(indexPath.row == 1)
        {
            [cell.label setText:@"退出登录"];
        }
        else if (indexPath.row == 3)
        {
            [cell.label setText:@"消息提醒"];
        }
//        cell.button.tag = indexPath.row;
//        [cell.button addTarget:self  action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        return  cell;
    }
    return nil;
}

#pragma mark - Table View Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 0)
    {
        UserDataSingleton *userData = [UserDataSingleton shareInstance];
        if(![userData isLogin])
        {
            [userData pleaseLogin:self];
            return;
        }
        
        [self.navigationController pushViewController:[RecvAddressViewController create] animated:YES];

    }
    else if(indexPath.row == 1)
    {
        [self animedShowMenue];
    }
    else if(indexPath.row == 2)
    {
        [self.navigationController pushViewController:[ContactMIDIController create] animated:YES];
    }
    else if(indexPath.row == 3)
    {
        [self.navigationController pushViewController:[MessageNotifyViewController create] animated:YES];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//-(void)clickButton : (UIButton *) sender
//{
//    if(sender.tag == 0)
//    {
//        UserDataSingleton *userData = [UserDataSingleton shareInstance];
//        if(![userData isLogin])
//        {
//            [userData pleaseLogin:self];
//            return;
//        }
//        
//        [self.navigationController pushViewController:[RecvAddressViewController create] animated:YES];
//    }else if(sender.tag == 1)
//    {
//        [self animedShowMenue];
//    }else if(sender.tag == 2)
//    {
//        
//    }
//    else if(sender.tag == 3)
//    {
//        [self.navigationController pushViewController:[MessageNotifyViewController create] animated:YES];
//    }
//}

- (void) animedShowMenue
{
    if(self.isAnimed || self.isOpenMenu)
        return;
    self.menuView.hidden = NO;
    [UIView animateWithDuration:0.4 delay:0.1 options:UIViewAnimationOptionCurveEaseInOut animations:^
     {
         CGRect orgRect = self.menuItemView.frame;
         NSLog(@"Y is %f" , orgRect.origin.y );
         self.menuItemView.frame = CGRectMake(0, orgRect.origin.y - 101, [UIScreen mainScreen].bounds.size.width, 101);
         ///self.menuBottomConstraint.constant = 0;
         self.isAnimed = YES;
     }
                     completion:^(BOOL finished)
     {
         self.isAnimed = NO;
         self.menuBottomConstraint.constant = 0;
         self.isOpenMenu = YES;
         CGRect orgRect = self.menuItemView.frame;
         NSLog(@"menu size x : %f , y : %f" , orgRect.size.width , orgRect.size.height);
         NSLog(@"menu pos x : %f , y : %f" , orgRect.origin.x , orgRect.origin.y);
     }];
   
}

- (void) animedHideMenue
{
    if(self.isAnimed || !self.isOpenMenu)
        return;
    [UIView animateWithDuration:0.4 delay:0.1 options:UIViewAnimationOptionCurveEaseInOut animations:^
     {
         CGRect orgRect = self.menuItemView.frame;
         NSLog(@"Y2 is %f" , orgRect.origin.y );
         self.menuItemView.frame = CGRectMake(0, orgRect.origin.y + 101, [UIScreen mainScreen].bounds.size.width, 101);
         self.isAnimed = YES;
     }
                     completion:^(BOOL finished)
     {
         self.isAnimed = NO;
         self.menuBottomConstraint.constant = -101;
         self.isOpenMenu = NO;
         self.menuView.hidden = YES;
     }];
   
}

- (IBAction)clickExit:(id)sender
{
    [[UserDataSingleton shareInstance] loginOut];
    [self animedHideMenue];
 
    UITabBarController * vc = [self.navigationController.viewControllers firstObject];
    
    [vc setSelectedIndex:3];
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (IBAction)clickCancel:(id)sender
{
    [self animedHideMenue];
}

- (void)tapBg : (UITapGestureRecognizer*)gesture
{
    [self animedHideMenue];
}

@end












